import obligor from '../../api/obligor.js'
import config from '../../static/config.js'
import date from '../../utils/date.js'
import mayDebt from '../../api/mayDebt.js'
import personal from '../../api/personal.js'
import moment from 'moment'
const state = {
	list: [],
	realInfo: [],
	total: 0,
	status: 'loadmore',
	information:'',
	rows:[],
	confirmedList:[],
	readingText:{},
}
const mutations = {
	CHANGE_STATUS(state, data) {
		state.status = data
	}


}
const actions = {
	//获取轮播图 
	GET_BANNER_DATA({
		state
	}) {
		obligor.bannerPost().then(res => {
			if (res) {
				state.list=[]
				res.data.map(item => {
					state.list.push({
						image: config.imgUrl + item.image,
						id:item.news_id
					})
				})
			}
		})
	},
	//获取资讯列表
	GET_NEW_DATA({
		state
	}, data) {
		obligor.newsListPost(data).then(res => {
			if (res) {
				res.data.rows.map(item => {
					item.createtime = date.transformation(item.createtime)
				})
				state.realInfo = res.data.rows
				state.total = res.data.total
				uni.stopPullDownRefresh()
				uni.hideLoading();
			}
		})
		
	},
	SUBSCRIBE({state}){
	obligor.waitDebtPost().then(res=>{
		if(res){
			res.data.map(item=>{
				item.loan_date=moment(item.loan_date).format('YYYY.MM.DD')
				item.repay_date=moment(item.repay_date).format('YYYY.MM.DD')
				item.createtime=moment(item.createtime*1000).format('YYYY.MM.DD HH:mm:ss')
			})
		state.rows=res.data	
		}
	})	
	},
	//首页弹框阅读文字
	GET_BULLET_BOXTEXT({state}){
		 personal.helpCenterPost({type:4}).then(res=>{
			 state.readingText=res.data
		 })
	},
	GET_NEW_DATA_MORE({
		state,
		dispatch
	}, data) {
		obligor.newsListPost(data).then(res => {
			if (res) {
				res.data.rows.map(item => {
					item.createtime = date.transformation(item.createtime)
				})
				state.realInfo = state.realInfo.concat(res.data.rows)
				if (data.page * data.page_size < res.total) {
					// dispatch('GET_BANNER_DATA', "loadmore")
				} else {
					// dispatch('GET_BANNER_DATA', 'nomore')
				}
			}
		})

	},
	DETAILS_INFO({state},id){
		obligor.newInfoPost({id:id}).then(res=>{
			if(res){
			 state.information=res.data	
			}
		})
	},
	//获取待确认借条
	GET_CONFIRMED({state}){
		let info={
			name:'',
			order:'loan_money',
			state:0,
			page:1,
			page_size:10
		}
		let values=JSON.parse(JSON.stringify(info))
		mayDebt.debtListPost(info).then(res=>{
            values.page_size=res.data.total
			mayDebt.debtListPost(values).then(res=>{
			res.data.rows.map(item=>{
				item.loan_date=moment(item.loan_date).format('YYYY.MM.DD')
				item.repay_date=moment(item.repay_date).format('YYYY.MM.DD')
				item.createtime=moment(item.createtime*1000).format('YYYY.MM.DD HH:mm:ss')
			})
			state.confirmedList=res.data.rows
			})
		})
	}
}
export default {
	namespaced: true,
	state,
	mutations,
	actions
}
